// pages/secondHand/secondHand.js
var app = getApp();
const util = require('../../utils/util.js');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    httpApiImg: app.globalData.httpApiImg,
    uploadUrl: app.globalData.uploadUrl,
    showCoverInp: true,
    currentPage: 1,
    secondList: [],
    hasData: true,
    title: '',
    wholeMine:'',
    userInfo: '',
    loadSuccess: false
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.showLoading()
    this.setData({
      userInfo: JSON.parse(wx.getStorageSync('userInfo'))
    })
    if (options.page == 'wholeMine') {
      this.setData({
        wholeMine: true
      })
      wx.setNavigationBarTitle({
        title: '我的二手',
      })
    }
  },
  inputFocus: function (e) {
    this.setData({
      showCoverInp: false
    })
  },
  inputBlur: function (e) {
    if(!this.data.title){
      this.setData({
        showCoverInp: true
      })
    }
    
  },
  // 发布
  publish: function(){
    wx.navigateTo({
      url: '/pages/secondPublish/secondPublish',
    })
  },
  // 详情
  detail: function (e) {
    wx.navigateTo({
      url: '/pages/secondDetail/secondDetail?id='+e.currentTarget.dataset.id,
    })
  },
  inputSearch: function(e){
    this.setData({
      title: e.detail.value,
      ['currentPage']: 1,
      secondList: []
    })
    this.getSecondHand()
  },
  // 查询数据
  getSecondHand: function () {
    var _this = this;
    var param={
      currentPage: _this.data.currentPage,
      pageNumber: 10,
      title: _this.data.title
    };
    if (this.data.wholeMine){
      param.userId = this.data.userInfo.id
    }
    wx.request({
      url: app.globalData.httpApiCar + 'secondHandInfo/page',
      data: param,
      success: function (res) {
        _this.setData({
          loadSuccess: true
        })
        wx.hideLoading()
        if (res.data.success) {
          wx.hideNavigationBarLoading() //完成停止加载
          wx.stopPullDownRefresh() //停止下拉刷新
          var second = res.data.data.pp;
          for (var i = 0; i < second.length; i++) {
            if (second[i].backupA) {
              second[i].backupA = second[i].backupA.split(",");
            }
            if (second[i].createTime) {
              second[i].createTime = util.getDays(second[i].createTime)
            }
            if (second[i].describtion) {
              if (second[i].describtion.length > 70) {
                second[i].describtion = second[i].describtion.substring(0, 70) + "...";
              }
            }
            _this.data.secondList.push(second[i])
          }
          _this.setData({
            secondList: _this.data.secondList
          })
          if (_this.data.secondList.length == res.data.data.page.totalNumber) {
            _this.setData({
              hasData: false
            })
          }
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.setData({
      ['currentPage']: 1,
      secondList: []
    })
    this.getSecondHand()
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.showNavigationBarLoading();
    this.setData({
      ['currentPage']: 1,
      secondList: []
    })
    this.getSecondHand();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    if (this.data.hasData) {
      this.setData({
        ['currentPage']: this.data.currentPage + 1
      })
      this.getSecondHand();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})